/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import bean.Product;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;
import dao.GenericDAO;
import java.sql.PreparedStatement;

/**
 *
 * @author Eletro Shopping
 */
public class ProductDAO extends GenericDAO{
    
        
    private static final long serialVersioUID = 1L;

    public ProductDAO() {
    }
    
    public int addProduct(Product prod) throws SQLException{
        prod.setId(getNextId("PRODUCTS"));
        String query = "INSERT INTO PRODUCTS(ID,NAME,PRICE,STOCK) VALUES (?,?,?,?)";
        executeCommand(query, prod.getId(), prod.getName(), prod.getPrice(), prod.getStock());
        return prod.getId();
    }
    
    public void removeProduct(int idProduct) throws SQLException{
        executeCommand("DELETE FROM PRODUCTS WHERE ID = ?", idProduct);
    }
    
    public void updateProducts(Product prod) throws SQLException{
        String query = "UPDATE PRODUCTS SET NAME = ?, PRICE = ?, STOCK = ? WHERE ID = ?";
        executeCommand(query, prod.getName(), prod.getPrice(), prod.getStock(), prod.getId());
    }
    
     public Product getProduct(int idProduct) throws SQLException {
        ResultSet rs = executeQuery("SELECT * FROM PRODUCTS WHERE ID = ?", idProduct);
        Product prod = null;
        if (rs.next()) {
           prod = populateProduct(rs);
        }
        rs.close();
        return prod;
    }
    
    public static Product populateProduct(ResultSet rs) throws SQLException{
        Product toReturn = new Product();
        toReturn.setId(rs.getInt("ID"));
        toReturn.setName(rs.getString("NAME"));
        toReturn.setPrice(rs.getFloat("PRICE"));
        toReturn.setStock(rs.getInt("STOCK"));
        return toReturn;
    }
    
    public List<Product> getallProduct() throws SQLException{
        List<Product> toReturn = new LinkedList<Product>();
        ResultSet rs = executeQuery("SELECT * FROM PRODUCTS");
        while(rs.next()){
            toReturn.add(populateProduct(rs));
        }
        return toReturn;
    }
    
}
